import { getMusicUrl, getPlaylistDetail, getSongDetail } from "@/api"

export default {
  state: {
    list: [],
    song: {}, // 当前播放的歌曲
    musicUrls: [],
    isPlay: false,
    currentTime: 0,
    // currentPlayMusic: {}
  },
  mutations: {
    SET_PLAYER_LIST(state, data) {
      state.list = data
    },
    SET_PLAYER_DATA(state, data) {
      Object.keys(data).forEach(function (key) {
        state[key] = data[key]
      })
    }
  },
  actions: {
    playMusic({ commit, state }, id) {
      commit('SET_PLAYER_DATA', { isPlay: false, currentTime: 0 })
      getSongDetail(id).then(res => {
        commit('SET_PLAYER_DATA', { song: res.songs[0] })
      })

      getMusicUrl(id).then(res => {
        commit('SET_PLAYER_DATA', { musicUrls: res.data, isPlay: true })
      })
    },
    playPlaylist({ commit, state, dispatch }, id) {
      getPlaylistDetail(id).then(res => {
        console.log(' res: ', res)
        commit('SET_PLAYER_LIST', res.playlist.tracks);
        dispatch('playMusic', res.playlist.tracks[0].id)
      })
    }
  }
}